import request from './request'
import { useUserStore } from '../store/modules/user'
import { AxiosPromise } from 'axios'

/**
 * 登录
 * @param data - 请求参数
 * @param data.password - 登录密码/验证码
 * @param data.phone - 手机号/登录账号
 * @param data.rememberMe - 记住我：true-是，false-否
 * @param data.type - 登录类型：1-验证码，2-账号/手机号密码
 */
export function login(data: {
  password: string
  phone: string
  rememberMe: boolean
  type: 1 | 2
}): AxiosPromise<
  APIResponse<{ expireIn: number; refreshToken: string; tokenName: string; tokenValue: string }>
> {
  return request({
    url: '/system/login/',
    method: 'post',
    data,
  })
}

// 退出登录
export function loginOut() {
  const token = {
    'access-token': useUserStore().row.tokenValue,
  }
  return request({
    url: '/system/login/logout',
    method: 'get',
    headers: token,
  })
}
// 短信登录验证码
export function loginCode(params: { phone: string }) {
  return request({
    url: '/sms/loginCode',
    method: 'get',
    params,
  })
}
// 短信注册验证码
export function registerCode(params: { phone: string }) {
  return request({
    url: '/sms/registerCode',
    method: 'get',
    params,
  })
}
// 更新验证码
export function updateCode(params: { phone: string }) {
  return request({
    url: '/sms/updateCode',
    method: 'get',
    params,
  })
}
// 未登录更新
export function notLoginUpdatePassword(data: {
  phone: string
  newPassword: string
  oldPassword: string
  type: number
}) {
  return request({
    url: '/system/account/notLoginUpdatePassword',
    method: 'PUT',
    data,
  })
}
// 注册
export function register(data: any) {
  return request({
    url: '/system/account/register',
    method: 'post',
    data,
  })
}

/**
 * 获取账号关联所有企业
 */
export function getCustomer(): AxiosPromise<
  APIResponse<AccountAPIResponse.AccountConnectEnterprise>
> {
  return request({
    url: '/system/login/getCustomer',
    method: 'get',
  })
}

// 选择企业
export function getCustomerCode(params) {
  const token = {
    'access-token': useUserStore().row.tokenValue,
  }
  return request({
    url: '/system/login/getCustomerCode',
    method: 'get',
    params,
    headers: token,
  })
}
// 注册企业
export function registerCustomer(data) {
  const token = {
    'access-token': useUserStore().row.tokenValue,
  }
  return request({
    url: '/system/customer/register',
    method: 'post',
    data,
    headers: token,
  })
}
// 刷新token
export function refreshToken(params) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/login/refreshToken',
    method: 'get',
    headers: token,
    params,
  })
}
// 获取登录账号信息
export function getInfoUser() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/account/getInfo',
    method: 'get',
    headers: token,
  })
}
// 修改个人信息
export function updateInfo(data) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/account/updateInfo',
    method: 'PUT',
    data,
    headers: token,
  })
}
// 修改密码
export function updatePassword(data) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/system/account/updatePassword',
    method: 'PUT',
    data,
    headers: token,
  })
}

/**
 * 获取更改密码的验证码
 * @param phone - 手机号
 */
export const getUpdatePwdVerifyCode = (phone: string): AxiosPromise<APIResponse> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/sms/updateCode',
    method: 'get',
    params: {
      phone,
    },
    headers: token,
  })
}

/**
 * 获取单点登录的用户信息
 * @param data - 请求参数
 */
export const getSSOUserInfo = (data: {
  'RSA-TOKEN': string
  LINK: string
}): AxiosPromise<NewAPIResponse<AccountAPIResponse.SSOUserInfo>> => {
  return request({
    url: '/sso/resolve-rsa',
    method: 'get',
    params: data,
  })
}
